<?php

namespace App\Admin\Controllers\Dedusting;

use App\Models\DedustingAdjustment;
use App\Models\DedustingDevice;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use App\Handlers\IotCardHandler;

class AdjustmentController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('调适步骤列表')
            ->description('')
            ->body($this->grid());
    }

    protected function grid()
    {
        return new Grid(DedustingAdjustment::with(['device']), function (Grid $grid) {
            $grid->column('device.name', '设备');
            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '修改时间');
        });
    }

    public function show($id, Content $content)
    {
        return $content->header('步骤')
            ->description('详情')
            ->body(Show::make($id, new DedustingAdjustment(), function (Show $show) {
                $show->field('device.name', '设备');

                $show->detail('步骤详情')->unescape()->as(function ($detail) {
                    $d = json_decode($detail, true);
                    $str = '';
                    foreach ($d as $key => $one) {
                        $oneStr = '<div>'.$one['title'].'</div><div>'.$one['desc'].'</div>';
                        $images = json_decode($one['image'],true);
                        foreach ($images as $img) {
                            $oneStr .= '<img style="width:200px;height:200px" src="'.$img.'" />';
                        }
                        $str .= $oneStr;
                    }

                    return "<pre>{$str}</pre>";
                });

                $show->field('created_at', '创建时间');
                $show->field('updated_at', '修改时间');
            }));
    }

    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('新建步骤')
            ->description('创建')
            ->body($this->form());
    }

    public function form(){
        return Form::make(new DedustingAdjustment(), function (Form $form) {
            $devices = DedustingDevice::pluck('name','id');
            $form->select('device_id', '设备')->options($devices)->required();

            $form->array('detail', '步骤详情', function ($table) {
                $table->text('title', '标题');
                $table->textarea('desc', '描述');
                $table->multipleImage('image', '图片')
                    ->move(DedustingAdjustment::IMAGE_PATH)
                    ->uniqueName()
                    ->saveFullUrl()
                    ->saving(function ($paths) {
                        return json_encode($paths, JSON_UNESCAPED_SLASHES);
                    })
                    ->autoUpload();
            })->saving(function ($v) {
                return json_encode($v, JSON_UNESCAPED_SLASHES);
            });

            $form->hidden('user_id')->value(Admin::user()->id);
            $form->saving(function (Form $form) {
                $form->user_id = Admin::user()->id;
            });
        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('步骤编辑')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }
}